const log4js = require('log4js');
log4js.configure({
    appenders: {
        console: {
            type: 'console',
        },
        out: { 
            type: 'stdout' 
        }
        // dateFile: {
        //     type: 'dateFile', 
        //     filename: './log/app.log', 
        //     pattern: '.yyyy-MM-dd', 
        //     alwaysIncludePattern: true, 
        //     keepFileExt: true
        // },
        // file: {
        //     type: 'file',
        //     filename: './log/app.log',
        //     maxLogSize: 100*1024*1024,
        //     keepFileExt: true,
        // }
    },
    categories: {
        default: {
            appenders: ['console'],
            level: 'info',
        },
        console: {
            appenders: ['console'],
            level: 'info',
        }
    }
});
const logger = log4js.getLogger();

//把console的日志转换成log4js日志
console.log = logger.info.bind(logger);
console.info = logger.info.bind(logger);
console.debug = logger.debug.bind(logger);
console.error = logger.error.bind(logger);
console.trace = logger.trace.bind(logger);

//输出express日志
function connectLogger(){
    return log4js.connectLogger(logger);
}

process.on('SIGINT', (code) => {
    log4js.shutdown((error) => {
        if(error){
            console.log(error);
        }
    });
});

exports.connectLogger = connectLogger;
exports.logger = logger;